#include "stdafx.h"
#include "MMap.h"

CMMap::CMMap()
{
	m_BGImg[0] = cvLoadImage("..\\Data\\image\\map\\Theme1\\BG01\\BG1-hd.png");
	m_MapImg[0] = cvLoadImage("..\\Data\\image\\map\\Theme1\\BG01\\MAP1-hd.png");
	m_StartPos[0] = cvPoint(120, 180);
	m_EndPos[0] = cvPoint(840, 180);
	m_StartDir[0] = 2;
	m_GateNum = 0;
}


CMMap::~CMMap()
{
}

void CMMap::LoadBGImg()
{
	char filename[] = "..\\Data\\image\\map\\Theme1\\BG00\\BG-hd.png";
	for (int i = 0; i < GATE_COUNT; i++)
	{
		sprintf(filename, ("..\\Data\\image\\map\\Theme1\\BG%2d\\BG-hd.png"), i + 1);
		m_BGImg[i] = cvLoadImage(filename);
	}
}

void CMMap::LoadMapImg()
{
	char filename[] = "..\\Data\\image\\map\\Theme1\\BG00\\Map-hd.png";
	for (int i = 0; i < GATE_COUNT; i++)
	{
		sprintf(filename, ("..\\Data\\image\\map\\Theme1\\BG%2d\\Map-hd.png"), i + 1);
		m_BGImg[i] = cvLoadImage(filename);
	}
}

void CMMap::LoadMap()
{
}

void CMMap::LoadDir()
{
}

void CMMap::LoadPos()
{
}

void CMMap::Init()
{
	LoadPos();
	LoadDir();
	LoadMapImg();
	LoadBGImg();
}

void CMMap::Release()
{
	for (int i = 0; i < GATE_COUNT; i++)
	{
		cvReleaseImage(&m_BGImg[i]);
		cvReleaseImage(&m_MapImg[i]);
	}
}

int * CMMap::GetMap(int row)
{
	return m_Map[m_GateNum][row];
}

int CMMap::GetDir()
{
	return m_StartDir[m_GateNum];
}


CvPoint CMMap::GetStartPos()
{
	return m_StartPos[m_GateNum];
}

CvPoint CMMap::GetEndPos()
{
	return m_EndPos[m_GateNum];
}

IplImage * CMMap::GetBGImg()
{
	return m_BGImg[m_GateNum];
}

IplImage * CMMap::GetMapImg()
{
	return m_MapImg[m_GateNum];
}

int CMMap::FindDir(CvPoint Pos, int dir)
{
	CvPoint Dir;
	Dir.x = Dir.y = 0;
	switch (dir)
	{
	case 0:Dir.y = -VISUAL_DIS; break;
	case 1:Dir.x = VISUAL_DIS; break;
	case 2:Dir.y = VISUAL_DIS; break;
	case 3:Dir.x = -VISUAL_DIS; break;
	}
	CvScalar front = Tool->FindScalar(m_MapImg[m_GateNum], Pos.x + Dir.x, Pos.y + Dir.y);
	if (front.val[0] != 0 || front.val[1] != 0 || front.val[2] != 0)
		return dir;
	Dir.x = 0;
	Dir.y = 0;
	switch (dir)
	{
	case 0:
		Dir.x = -VISUAL_DIS;
		front = Tool->FindScalar(m_MapImg[m_GateNum], Pos.x + Dir.x, Pos.y + Dir.y);
		if (front.val[0] == front.val[1] && front.val[2] == front.val[1] && front.val[0] == 0)
			return 1;
		else
			return 3;
		break;
	case 1:
		Dir.y = -VISUAL_DIS;
		front = Tool->FindScalar(m_MapImg[m_GateNum], Pos.x + Dir.x, Pos.y + Dir.y);
		if (front.val[0] == front.val[1] && front.val[2] == front.val[1] && front.val[0] == 0)
			return 2;
		else
			return 0;
		break;
	case 2:
		Dir.x = VISUAL_DIS;
		front = Tool->FindScalar(m_MapImg[m_GateNum], Pos.x + Dir.x, Pos.y + Dir.y);
		if (front.val[0] == front.val[1] && front.val[1] == front.val[2] && front.val[0] == 0)
			return 3;
		else
			return 1;
		break;
	case 3:
		Dir.y = VISUAL_DIS;
		front = Tool->FindScalar(m_MapImg[m_GateNum], Pos.x + Dir.x, Pos.y + Dir.y);
		if (front.val[0] == front.val[1] && front.val[2] == front.val[1] && front.val[0] == 0)
			return 0;
		else
			return 2;
		break;
	}
}

void CMMap::SetTool(CTool *tool)
{
	Tool = tool;
}
